#include "courseselection.h"
#include "ui_courseselection.h"
#include <QCheckBox>

CourseSelection::CourseSelection(QWidget *parent): QDialog(parent), ui(new Ui::CourseSelection)
{
    ui->setupUi(this);
    initTable();
}

CourseSelection::~CourseSelection()
{
    delete ui;
}

void CourseSelection::setCourseList(const QList<Course> courseList)
{
    // 清空表格
    int rowCount = ui->courseTable->rowCount();
    for(int row = 0; row < rowCount; row++){
        ui->courseTable->removeRow(0);
    }
    this->select.clear();
    this->courseList.clear();


    this->courseList = courseList;
    for (Course c: courseList) {
        appendOneRow(c.getId(), c.getName(), c.getSX_str(), c.getXZ_str(), c.getXF(), c.getXS(), c.getTime());
    }
    lineSpacing();
}

void CourseSelection::initTable()
{
    ui->courseTable->setColumnCount(8);   //设置 8 列表格
    ui->courseTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->courseTable->verticalHeader()->setVisible(false);   //隐藏默认的序号
    QStringList header;
    header<<""<<"编号"<<"名称"<<"属性"<<"性质"<<"学分"<<"学时"<<"开设时间";
    ui->courseTable->setHorizontalHeaderLabels(header);
    ui->courseTable->setColumnWidth(0, 20);    // 选择
    ui->courseTable->setColumnWidth(1, 40);    // 编号
    ui->courseTable->setColumnWidth(2, 200);   // 名称
    ui->courseTable->setColumnWidth(3, 50);   // 属性
    ui->courseTable->setColumnWidth(4, 130);    // 性质
    ui->courseTable->setColumnWidth(5, 60);    // 学分
    ui->courseTable->setColumnWidth(6, 60);    // 学时
    ui->courseTable->setColumnWidth(7, 120);   // 开设时间

    //占满整个表格
    ui->courseTable->horizontalHeader()->setSectionResizeMode(7, QHeaderView::Stretch);
    ui->courseTable->horizontalHeaderItem(0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}

void CourseSelection::appendOneRow(int id, QString name, QString sx, QString xz, double xf, int xs, QString time)
{
    int count = ui->courseTable->rowCount();
    ui->courseTable->setRowCount(count + 1);

    QTableWidgetItem* idItem = new QTableWidgetItem(QString::number(id));
    QTableWidgetItem* nameItem = new QTableWidgetItem(name);
    QTableWidgetItem* sxItem = new QTableWidgetItem(sx);
    QTableWidgetItem* xzItem = new QTableWidgetItem(xz);
    QTableWidgetItem* xfItem = new QTableWidgetItem(QString::number(xf));
    QTableWidgetItem* xsItem = new QTableWidgetItem(QString::number(xs));
    QTableWidgetItem* timeItem = new QTableWidgetItem(time);

    QCheckBox *checkBox = new QCheckBox(this);
    connect(checkBox, &QCheckBox::toggled, [this, count](bool checked) {
        if (checked) {
            select[count] = ui->courseTable->item(count, 1)->text().toInt();
        } else {
            select[count] = 0;
        }
    });

    ui->courseTable->setCellWidget(count, 0, checkBox);
    ui->courseTable->setItem(count, 1, idItem);
    ui->courseTable->setItem(count, 2, nameItem);
    ui->courseTable->setItem(count, 3, sxItem);
    ui->courseTable->setItem(count, 4, xzItem);
    ui->courseTable->setItem(count, 5, xfItem);
    ui->courseTable->setItem(count, 6, xsItem);
    ui->courseTable->setItem(count, 7, timeItem);

    select[count] = 0;
}

void CourseSelection::lineSpacing()
{
    int rowCount = ui->courseTable->rowCount();
    int colCount = ui->courseTable->columnCount();

    for(int row = 0; row < rowCount; row++)
    {
        for(int col = 1; col < colCount; col++)
        {
            ui->courseTable->item(row, col)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        }
    }
}

void CourseSelection::on_courseTable_clicked(const QModelIndex &index)
{
    int row = index.row();

    QCheckBox* checkBox = qobject_cast<QCheckBox*>(ui->courseTable->cellWidget(row, 0));

    if (select[row] == 0) {
        select[row] = ui->courseTable->item(row, 1)->text().toInt();
        checkBox->setCheckState(Qt::Checked);
    } else {
        select[row] = 0;
        checkBox->setCheckState(Qt::Unchecked);
    }
}

void CourseSelection::on_selectAll_clicked()
{
    for (int row = 0; row < select.size(); row++) {
        QCheckBox* checkBox = qobject_cast<QCheckBox*>(ui->courseTable->cellWidget(row, 0));
        checkBox->setCheckState(Qt::Checked);
        select[row] = ui->courseTable->item(row, 1)->text().toInt();
    }
}

void CourseSelection::on_unselectAll_clicked()
{
    for (int row = 0; row < select.size(); row++) {
        QCheckBox* checkBox = qobject_cast<QCheckBox*>(ui->courseTable->cellWidget(row, 0));
        checkBox->setCheckState(Qt::Unchecked);
        select[row] = 0;
    }
}

void CourseSelection::on_selectAll_bixiu_clicked()
{
    for (int row = 0; row < select.size(); row++) {
        QCheckBox* checkBox = qobject_cast<QCheckBox*>(ui->courseTable->cellWidget(row, 0));

        if (ui->courseTable->item(row, 3)->text() == "必修") {
            checkBox->setCheckState(Qt::Checked);
            select[row] = ui->courseTable->item(row, 1)->text().toInt();
        }
    }
}

void CourseSelection::on_unselectAll_gongxuan_clicked()
{
    for (int row = 0; row < select.size(); row++) {
        QCheckBox* checkBox = qobject_cast<QCheckBox*>(ui->courseTable->cellWidget(row, 0));

        if (ui->courseTable->item(row, 3)->text() == "公选") {
            checkBox->setCheckState(Qt::Unchecked);
            select[row] = 0;
        }
    }
}

void CourseSelection::on_submit_clicked()
{
    QList<Course> courseList;

    for (int row = 0; row < select.size(); row++) {
        if (select[row] != 0) {
            courseList.append(this->courseList[row]);
        }
    }
    if (courseList.isEmpty()) {
        done(QDialog::Rejected);
    } else {
        emit selectedCourseList(courseList);
        done(QDialog::Accepted);
    }
}

